//
//  YWColorPopoverAnimator.m
//  YWPhotoEditMaster
//
//  Created by jun peng on 2023/8/3.
//

#import "YWColorPopoverAnimator.h"
#import "YWColorPresentationController.h"

@interface YWColorPopoverAnimator ()<UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning>

@property(nonatomic,assign)BOOL isPresented;

@property(nonatomic,copy) void(^callBackBlock)(BOOL presented);

@end

@implementation YWColorPopoverAnimator


-(UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{
    
    YWColorPresentationController *presentation = [[YWColorPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
    
    presentation.presentedFrame = self.presentedFrame;
    
    return presentation;
    
}


//--------------UIViewControllerTransitioningDelegate
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    
    self.isPresented = YES;
    
    if (self.callBackBlock) {
        self.callBackBlock(self.isPresented);
    }
    
    return self;
}

-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    
    self.isPresented = NO;
    
    if (self.callBackBlock) {
        self.callBackBlock(self.isPresented);
    }
    
    return self;
}


//--------------UIViewControllerAnimatedTransitioning
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 0.3;
}


-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    //self.isPresented判断是弹出还是消失
    self.isPresented ? [self animationForPresentedView:transitionContext] : [self animationForDismissView:transitionContext];
}


//--------------自制方法
///*了解一下一下属性：*/
//imageView：只负责做动画、动画完成后要移除
//presentedView：弹出的view
//dismissView：消失的view
//transitionContext.containerView：负责做动画的容器
-(void)animationForPresentedView:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    // 1.取出弹出的View
    UIView *presentedView = [transitionContext viewForKey:UITransitionContextToViewKey];
    
    // 2.将prensentedView添加到containerView中
    [transitionContext.containerView addSubview:presentedView];

    // 3.执行动画
    presentedView.transform = CGAffineTransformMakeScale(1.0, 0);
    presentedView.layer.anchorPoint =  CGPointMake(0.5, 1);
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        presentedView.transform = CGAffineTransformIdentity;
    }completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
    
   
}

-(void)animationForDismissView:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    
    // 1.取出消失的View
    UIView *dismissView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        
        dismissView.transform = CGAffineTransformMakeScale(1.0, 0.00001);
    } completion:^(BOOL finished) {
        [dismissView removeFromSuperview];
        [transitionContext completeTransition:YES];
    }];
    
    
}






-(void)animatorCallBackBlock:(void(^)(BOOL presented))callBackBlock{
    
    self.callBackBlock = callBackBlock;
}


@end
